import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
let util = require('../../../utils/util.js');
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    contentImgsnum: 50,
    isHide: false,
    coverVideo: '', // '/org/StoreVO/2019/11/19/VAX0EIQuSjGCIt4NCEOQzw.mp4',
    uploadUrl: wx.getStorageSync('uploadUrl'),
    hideSup: true,
    typeIndex: 0,
    colorIndex: 0,
    typeList: [
      {
        level: 1,
        name: '大系列'
      }, {
        level: 2,
        name: '小系列'
      }, {
        level: 3,
        name: '颜色'
      }
    ],
    loading: false,
    isIphoneX: app.globalData.isIphoneX,
    coverImgs: [],
    contentImgs: [],
    operationImgs: [],
    modelImgs: [],
    extraNodes: [
      {
        type: "after",
        dragId: "plus",
        slot: "plus",
        fixed: true
      }
    ],
    pageMetaScrollTop: 0,
    scrollTop: 0,
    video: [],
    devTypeList: [],
    devIndex: 0,
    modeIdIndex: 0,
    selectFormula: [],
    selectedFormula: [],
    formulaList: [],
    formulaGroupCode: '',

    HairColor: [],

    formulaRatioJson: [],
    checkboxFormulaRatio: [],
    isformulaCustomized: false, //是否定制配方类型

    isNewDevtypeUi: false,

    uiList: [
      {
        name: "最新版",
        url: "/pages/choose_series/index"
      },
      {
        name: "旧版本",
        url: "/pages/choose_colors/index"
      }
    ],
    UiIndex:1
  },
  UiChange(e){
    const index = e.detail.value
    this.setData({UiIndex:index})
  },
  onFormulaChange(e) {
    const selectFormula = e.detail;
    this.setData({
      selectFormula
    })
  },

  bindFormulaChange(e) {
    const formulaId = e.currentTarget.dataset.formulaId;
    const indexOfFormulaId = this.data.formulaList.findIndex(item => item.id == formulaId);
    const formulaRecord = this.data.formulaList[indexOfFormulaId];
    formulaRecord.code = e.detail.value;
    this.setData({
      [`formulaList[${indexOfFormulaId}]`]: formulaRecord
    })
  },

  getHairColorEnum() {
    // 获取发色序号
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/comm/system/iot.IotEnum$HairColor/enum/full',
      method: 'GET'
    }, (res) => {
      this.setData({ HairColor: res.list })
    })
  },
  radioChange(e) {
    console.log(e.detail.value)
  },
  getFormulaDic() {
    //色板模式
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/comm/beanCrud/sys.DictVO/find',
      method: 'POST',
      data: {
        groupCode: this.data.formulaGroupCode,
        pageSize: 100,
      },
      loading: false
    }, (res) => {
      var list = res.list
      list = list.map(item => {
        return {
          ...item,
          dicId: item.id,
          code: ""
        }
      })
      console.log('formulaList', list);
      console.log('this.data.formulaGroupCode', this.data.formulaGroupCode);

      this.setData({
        formulaList: list
      })


      this.setData({
        modeIdList: res.list
      })
      console.log(res, 'modelList')
    })
  },

  bindCatch(e) {

  },

  onLoad: function (options) {
    if (options && options.id) {
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on && eventChannel.on('deviceItem', data => {
        const formulaRatioJson = data.formulaRatioJson ? JSON.parse(data.formulaRatioJson) : []
        const checkboxFormulaRatio = formulaRatioJson.map(e => e.cnum)
        var typeIndex = this.data.typeList.findIndex(item => item.level == data.level);
        const UiIndex = this.data.uiList.findIndex(e=>e.url == data.url)
        this.setData({
          id: options.id,
          cententDetail: data,
          video: data.videoJson != '' ? JSON.parse(data.videoJson) : [],
          contentImgs: data.modelJson != '' ? JSON.parse(data.modelJson) : [],
          operationImgs: data.operationJson,
          modelImgs: [{ imgPath: data.modelImg }],
          coverImgs: [{
            imgName: '',
            imgPath: data.icon,
            fixed: true
          }],
          typeIndex: typeIndex,
          //isformulaCustomized: data.formulaCustomized == 1,
          checkboxFormulaRatio,
          UiIndex:UiIndex,
          formulaRatioJson
        }, () => {
          // this.getDeviceTypeVO();//获取设备类型
          //this.getPidColorList();
          //this.getColorFindList();
          this.getModeIdList();//模式列表
        })
      })
    }
    // this.getFormulaDic();
    this.getDeviceTypeVO();//获取设备类型
    if (!options.id) {
      this.getModeIdList();//模式列表
    }
    //获取发色序号
    this.getHairColorEnum()

    this.dragcoverImgs = this.selectComponent('#dragcoverImgs');
    this.dragcoverImgs.init();


    this.dragcontentImgs = this.selectComponent('#dragcontentImgs');
    this.dragcontentImgs.init();



    // this.dragoperationImgs = this.selectComponent('#operationImgs');
    // this.dragoperationImgs.init();

    this.dragModelImgs = this.selectComponent('#dragModelImgs');
    this.dragModelImgs.init();


  },

  onShow: function () {
  },


  //选择商品图片
  chooseGoodsImg: function (e) {
    this.setData({
      imgs: this.coverImgs.data.imgs,
      isHide: true
    })
  },

  deleteGoodsImg: function (e) {
    this.setData({
      imgs: this.coverImgs.data.imgs
    })
  },

  deloriginGoodsImg: function () {
    this.setData({
      originImgs: this.details.data.originImgs
    })
  },

  completefun: function () {
    console.log('wanc')
    this.setData({
      isHide: false
    })
  },

  //选择介绍图片
  choosecontentImgs: function (e) {
    this.setData({
      imgs: this.coverImgs.data.imgs
    })
  },

  deletecontentImgs: function (e) {
    this.setData({
      imgs: this.coverImgs.data.imgs
    })
  },

  delorigincontentImgs: function () {
    this.setData({
      originImgs: this.details.data.originImgs
    })
  },




  //介绍视频回调
  contentVideoCallback: function () {
    console.log(this.contentVideo, 'this.contentVideo')
    this.setData({
      contentVideo: this.contentVideo.data.video,
      contentVideoTime: '00:' + this.contentVideo.data.duration
    })
  },






  goback: function () {
    wx.navigateBack({

    })
  },




  //表单提交
  formSubmit: function (e) {
    var formdata = e.detail.value;
    if (this.data.typeIndex == 0) {
      formdata.pid = 0
      const modeIdList = this.data.modeIdList
      formdata.modeId = modeIdList.length && modeIdList[this.data.modeIdIndex].id
    } else if (this.data.typeIndex == 1) {
      formdata.pid = this.data.colorIndex == undefined ? '' : this.data.colorList[this.data.colorIndex].id
    } else {
      formdata.pid = this.data.colorIndex == undefined ? '' : this.data.colorList[this.data.colorIndex].id
      formdata.videoImgs = this.data.video
      formdata.modelImgs = this.data.contentImgs
      // formdata.operationImgs = this.data.operationImgs
      formdata.operationJson = formdata.operationImgs;
      formdata.operationImgs = "";
      formdata.note = formdata.content;
    }
    formdata.level = this.data.typeList[this.data.typeIndex].level
    formdata.typeId = this.data.devTypeList[this.data.devIndex].id
    if (this.data.typeList[this.data.typeIndex].level == 3) {
      formdata.modelImg = this.data.modelImgs[0].imgPath;
    }

    if (this.data.coverImgs.length > 0) {
      formdata.icon = this.data.coverImgs[0].imgPath
    }

    if (formdata.pid > 0 && !formdata.typeId || formdata.typeId == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择设备类型',
      })
      return
    }

    if (formdata.colorName == '') {
      wx.showToast({
        icon: 'none',
        title: '名称不能为空',
      })
      return
    }

    if (!formdata.code) {
      wx.showToast({
        icon: 'none',
        title: '编号不能为空'
      })
      return
    }

    if (formdata.coverImgs && formdata.coverImgs.length < 1) {
      var title = '请上传模特图片'
      if (this.data.typeIndex) {
        title = '请上传图标'
      }
      wx.showToast({
        icon: 'none',
        title
      })
      return
    }


    if (this.data.typeIndex != 0 && formdata.operationImgs && formdata.operationImgs.length < 1) {
      var title = '请上传操作图片'
      wx.showToast({
        icon: 'none',
        title
      })
      return
    }



    if (formdata.coverImgs && formdata.coverImgs.length < 1) {
      var title = '请上传模特图片'
      if (this.data.typeIndex) {
        title = '请上传图标'
      }
      wx.showToast({
        icon: 'none',
        title
      })
      return
    }

    if (this.data.selectFormula.length) {
      let selectFormulaList = [];
      this.data.selectFormula.map(item => {
        selectFormulaList = selectFormulaList.concat(this.data.formulaList.filter(filterItem => filterItem.id == item).map(formatItem => {
          let returnObj = {
            dicId: formatItem.id,
            code: formatItem.code,
            colorName: formatItem.name
          };
          if (this.data.selectedFormula.length > 0) {
            let findDic = this.data.selectedFormula.findIndex(item => item.dicId == formatItem.id);
            if (findDic != -1) {
              returnObj.id = this.data.selectedFormula[findDic].id
            }
          }
          console.log(1232113, returnObj);
          return returnObj;
        }))
      })
      formdata.formulaList = selectFormulaList;
    } else {
      formdata.formulaList = [];
    }

    //筛选定制配方设备类型选中的染膏头配置
    if (this.data.isformulaCustomized) {
      let formulaRatioList = this.data.formulaRatioJson
      formdata.formulaRatioList = []
      formulaRatioList.forEach(e => {
        if (this.data.checkboxFormulaRatio.indexOf(e.cnum) != -1 && e.o2 == 0) {
          formdata.formulaRatioList.push(e)
        }
      })
    }

    //如果是跳转新页面
    if (this.data.isNewDevtypeUi) {
      if (!formdata.lightNum) {
        var title = '请输入亮度序号'
        wx.showToast({
          icon: 'none',
          title
        })
        return
      }
      if (!formdata.lightNum) {
        var title = '请选择发色序号'
        wx.showToast({
          icon: 'none',
          title
        })
        return
      }
    }

    this.formRequest(formdata);
  },




  //请求
  formRequest: function (formdata) {
    var url = '/gds/protected/dyeColor/create';
    var title = "发布成功"
    //修改
    if (this.data.id) {
      url = "/gds/protected/dyeColor/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/colour_atla/list',
        })
      }, 1500)

    })
  },





  //**************轮播拖拽**************
  sortEnd(e) {
    this.setData({
      coverImgs: e.detail.listData
    });
  },
  change(e) {
    this.setData({
      coverImgs: e.detail.listData
    });
    this.dragcoverImgs.init()
  },
  sizeChange(e) {
    wx.pageScrollTo({ scrollTop: 0 })
    this.setData({
      size: e.detail.value
    });
    this.dragcoverImgs.init();
  },

  itemClick(e) {
    console.log(e);
  },
  toggleFixed(e) {
    let key = e.currentTarget.dataset.key;
    let { coverImgs } = this.data;
    coverImgs[key].fixed = !coverImgs[key].fixed
    this.setData({
      coverImgs: coverImgs
    });
    this.dragcoverImgs.init();
  },


  add(e) {
    let coverImgs = this.data.coverImgs;
    var that = this
    wx.chooseImage({
      count: that.data.coverImgsnum - that.data.coverImgs.length,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        that.uploadFile(res.tempFilePaths, 'coverImgs')
      }
    });
  },


  // *********介绍图录播 *********
  contentSortEnd(e) {
    this.setData({
      contentImgs: e.detail.listData
    });
  },
  contentChange(e) {
    this.setData({
      contentImgs: e.detail.listData
    });
    this.dragcontentImgs.init()
  },
  contentSizeChange(e) {
    wx.pageScrollTo({ scrollTop: 0 })
    this.setData({
      size: e.detail.value
    });
    this.dragcontentImgs.init();
  },

  contentItemClick(e) {
    console.log(e);
  },
  contentToggleFixed(e) {
    let key = e.currentTarget.dataset.key;
    let { contentImgs } = this.data;
    contentImgs[key].fixed = !contentImgs[key].fixed
    this.setData({
      contentImgs: contentImgs
    });
    this.dragcontentImgs.init();
  },

  contentAdd(e) {
    let contentImgs = this.data.contentImgs;
    var that = this
    wx.chooseImage({
      count: that.data.contentImgsnum - that.data.coverImgs.length,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        that.uploadFile(res.tempFilePaths, 'contentImgs')
      }
    });
  },

  modelImgsAdd(e) {
    let modelImgs = this.data.modelImgs;
    var that = this
    wx.chooseImage({
      count: that.data.contentImgsnum - that.data.modelImgs.length,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        that.uploadFile(res.tempFilePaths, 'modelImgs')
      }
    });
  },

  //  封面图
  modelImgSortEnd(e) {
    this.setData({
      modelImgs: e.detail.listData
    });
  },
  modelImgChange(e) {
    this.setData({
      modelImgs: e.detail.listData
    });
    this.dragModelImgs.init()
  },

  modelImgClick(e) {
    console.log(e);
  },



  // *********介绍图片 *********
  operationSortEnd(e) {
    this.setData({
      operationImgs: e.detail.listData
    });
  },
  operationChange(e) {
    this.setData({
      operationImgs: e.detail.listData
    });
    //this.dragoperationImgs.init()
  },
  contentSizeChange(e) {
    wx.pageScrollTo({ scrollTop: 0 })
    this.setData({
      size: e.detail.value
    });
    //this.dragoperationImgs.init();
  },

  operationItemClick(e) {
    console.log(e);
  },
  contentToggleFixed(e) {
    let key = e.currentTarget.dataset.key;
    let { operationImgs } = this.data;
    operationImgs[key].fixed = !operationImgs[key].fixed
    this.setData({
      operationImgs: dragoperationImgs
    });
    //this.dragoperationImgs.init();
  },

  operationtAdd(e) {
    let operationImgs = this.data.operationImgs;
    var that = this
    wx.chooseImage({
      count: that.data.contentImgsnum - that.data.coverImgs.length,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        that.uploadFile(res.tempFilePaths, 'operationImgs')
      }
    });
  },




  //上传图片
  uploadFile: function (pics, idName) {
    var that = this;
    var listData = this.data[idName]
    this.setData({
      loading: true
    })
    for (var i = 0; i < pics.length; i++) {
      (function (i) {
        //   setTimeout(function () { 
        wx.uploadFile({
          url: app.globalData.apiUrl + '/org/protected/comm/file/org.StoreVO/fileUpload',
          header: {
            "Authorization": wx.getStorageSync('Authorization'),
          },
          formData: {
            width: 750
          },
          filePath: pics[i],
          name: 'file',
          success: function (res) {
            if (typeof res.data == "string") {
              var resdata = JSON.parse(res.data)
            }
            if (resdata.success) {

              var item = {
                imgName: resdata.datas.vo.fileName,
                imgPath: resdata.datas.vo.relativeFilePath,
                fixed: false
              }
              listData.push(item)
              that.setData({
                [idName]: listData
              })

              //初始化组件
              if (idName == 'coverImgs') {
                that.dragcoverImgs.init();
              } else if (idName == 'contentImgs') {
                that.dragcontentImgs.init();
              } else if (idName == 'operationImgs') {
                //that.dragoperationImgs.init();
              } else if (idName == 'modelImgs') {
                that.dragModelImgs.init();
              }

              //最后一张关闭遮罩 
              console.log(i + 1 == pics.length, 'last')
              if (i + 1 == pics.length) {
                that.setData({
                  loading: false
                })
              }

            }
          },
          fail: function (res) {
            // wx.showToast({
            //   icon: 'none',
            //   title: '图片上传失败',
            // })
          }
        })
        //   }, (i + 1) * 800);
      })(i)
    }
  },

  scroll(e) {
    this.setData({
      pageMetaScrollTop: e.detail.scrollTop
    })
  },

  // 页面滚动
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },

  //如果是定制类型的设备
  showCustomCecipe() {
    //获取设备类型染膏头数量
    const devType = this.data.devTypeList[this.data.devIndex]
    if (this.isformulaCustomized || devType.formulaCustomized == 1) {
      _wxRequest.wxTonkenAjax({
        url: app.globalData.apiUrl + "/gds/protected/deviceType/getDyeList?dtoName=FormulaRatioDTO&dyeNum=" + devType.dyeNum,
        method: 'GET',
      }, (res) => {
        const dyeColorSetJson = JSON.parse(devType.dyeColorSetJson)
        let formulaRatioJson = this.data.formulaRatioJson;
        let newformulaRatioJson = res.list.map(x => {
          const e = dyeColorSetJson.find(e => x.cnum == e.cnum);
          const d = formulaRatioJson.find(d => x.cnum == d.cnum)
          const n = d || e || x
          return n
        })
        this.setData({ formulaRatioJson: newformulaRatioJson, isformulaCustomized: true })
      })
    }
  },

  changeFormulaRatio(event) {
    this.setData({
      checkboxFormulaRatio: event.detail,
    });
  },
  bindblurFormulaRatio(e) {
    const cnum = e.target.dataset.cnum
    const value = e.detail.value
    const formulaRatioJson = this.data.formulaRatioJson
    formulaRatioJson.forEach(e => {
      if (e.cnum == cnum) e.ratio = value
    })
    this.setData({ formulaRatioJson })
  },
  //选择类型
  bindPickerChange: function (e) {
    var val = e.detail.value
    this.setData({
      typeIndex: val,
      colorIndex: 0,
    }, () => {
      this.getPidColorList()
      this.getColorFindList();
      this.getModeIdList();//模式列表
    })
    if (val == 3) {
      this.dragcontentImgs = this.selectComponent('#dragcontentImgs');
      this.dragcontentImgs.init();
    }
    if (val == 2) {
      let dev = this.data.devTypeList[this.data.devIndex]

      this.setData({
        formulaGroupCode: dev.formulaGroupCode,
      })

      this.getFormulaDic();
      this.showCustomCecipe()
      console.log(e, 'bindPickerChange')
    } else {
      this.setData({
        formulaGroupCode: "a$gds$iot_dye_color$mode"
      })
    }


  },
  //选择色卡
  colorPickerChange: function (e) {
    var val = e.detail.value
    console.log(val, 'val')
    this.setData({
      colorIndex: val
    })
    this.getFormulaDic();
  },



  //选择设备类型
  devPickerChange: function (e) {
    var val = e.detail.value
    let devtype = this.data.devTypeList[val]

    this.setData({
      devIndex: val,
      formulaGroupCode: devtype.code,
      isformulaCustomized:devtype.formulaCustomized == 1
    }, () => {
      console.log(this.data.formulaGroupCode);
      this.bindPickerChange({ detail: { value: 0 } })
    })
  },

  modeIdPickerChange: function (e) {
    var val = e.detail.value
    this.setData({
      modeIdIndex: val
    })
  },




  //父级色卡列表
  getPidColorList: function () {
    var params = {
      level: this.data.typeList[this.data.typeIndex].level - 1,
      // pageNo: this.data.pageNo,
      pageSize: 1000,
    }

    if (this.data.devTypeList.length > 0) {
      params.typeId = this.data.devTypeList[this.data.devIndex].id
    }


    console.log(params, 'params')
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeColor/findList',
      method: 'POST',
      data: params,
      loading: false
    }, (res) => {
      let list = res.list;
      console.log('颜色list', list);
      if (this.data.id) {
        list.map((item, idx) => {
          if (item.id == this.data.cententDetail.pid) {
            this.setData({
              colorIndex: idx
            })
          }
        })
      }
      this.setData({
        colorList: list,
      });
    })
  },

  //查配方列表
  getColorFindList: function () {
    var params = {
      pid: this.data.id
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeColor/findList',
      method: 'POST',
      data: params,
      loading: false
    }, (res) => {
      if (this.data.id) {
        const selectedFormula = res.list;
        const formulaList = this.data.formulaList;

        selectedFormula.map(item => {
          const findDicIndex = formulaList.findIndex(dicItem => dicItem.id == item.dicId);
          const findByDicId = formulaList.filter(dicItem => dicItem.id == item.dicId);
          findByDicId[0].code = item.code;
          formulaList[findDicIndex] = findByDicId[0];
        })
        this.setData({
          formulaList,
          selectedFormula: selectedFormula,
          selectFormula: selectedFormula.map(item => item.dicId)
        })
      }
    })
  },

  addVideo() {
    // 这里有几个需求
    // 1.时间不超过10秒
    // 2.能够从手机相册选取也能视频拍摄
    // 3.拍摄的时候提示现在拍摄了几秒
    // 4.超过时长要像app一样自动停止提示
    const that = this
    wx.showActionSheet({
      itemList: ['从相册上传', '从相机拍摄'],
      success: function (res) {
        if (res.tapIndex === 0) {
          wx.chooseVideo({
            sourceType: ['album', 'camera'],
            maxDuration: 25,
            // compressed:false,
            camera: 'back',
            success: (res) => {
              if (res.duration > 25) {
                wx.showModal({
                  title: '视频时长不能大于25秒',
                  showCancel: false,
                })
                return
              }
              const videoPath = res.tempFilePath
              that.uploadVideo({ videoPath })
            }
          })
        } else {
          wx.navigateTo({
            url: '/pages/publish/camera/camera',
          })
          var onTakeVideo = function (res) {
            const videoPath = res.tempVideoPath
            const thumbPath = res.tempThumbPath
            that.uploadVideo({ videoPath, thumbPath })
            globalMitt.off('takeVideo', onTakeVideo)
          }
          globalMitt.on('takeVideo', onTakeVideo)
        }

      },
    })
  },

  uploadVideo(res) {
    wx.showLoading({
      title: '上传视频中',
      mask: true
    })
    // 上传视频并使用返回的路径作为视频的路径
    wx.uploadFile({
      url: app.globalData.apiUrl + '/gds/protected/comm/file/iot.DyeColorImgVO/fileUpload',
      header: {
        "Authorization": wx.getStorageSync('Authorization'),
      },
      filePath: res.videoPath,
      name: 'file',
      success: (response) => {
        if (response.statusCode == 200) {
          var data = JSON.parse(response.data) //后台返回的是字符串，转成对象格式
          if (data.success) {
            console.log('上传视频结果', data)
            var obj = {
              fileName: data.datas.vo.fileName,
              imgPath: data.datas.vo.relativeFilePath
            }
            // var relativeFilePath = data.datas.vo.relativeFilePath;
            console.log(this.data.video, obj)
            this.data.video.push(obj)
            this.setData({
              video: this.data.video
            })
          }

          wx.hideLoading()
        } else {
          wx.showToast({
            icon: 'none',
            title: '视频上传失败',
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          icon: 'none',
          title: '视频上传失败',
        })
      }
    })
  },


  deleteVideo(e) {
    var index = e.currentTarget.dataset.index
    if (index == 0 && this.data.video.length == 1) {
      //还剩最后一个 直接删除
      this.setData({
        video: []
      })
    } else {
      var video = this.data.video.splice(index, 1)
      this.setData({
        video: video
      })
    }
  },

  //获取设备类型
  getDeviceTypeVO() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/queryTypeList',
      method: 'POST',
      data: {
        pageNo: 100,
      },
      loading: false
    }, (res) => {
      var list = res.list
      if (this.data.id) {

        list.map((item, idx) => {
          if (item.id == this.data.cententDetail.typeId) {
            this.setData({
              devIndex: idx,
              formulaGroupCode: list[idx].formulaGroupCode
            }, () => {
              this.getPidColorList()
              this.showCustomCecipe()
              this.getFormulaDic();
              let timer = setTimeout(() => {
                this.getColorFindList();
                clearTimeout(timer)
              }, 1000)
            })
          }
        })
      }
      this.setData({
        devTypeList: list,
        // formulaGroupCode: this.data.devTypeList[this.data.devIndex].formulaGroupCode
      })
      console.log(this.data.devTypeList, '-----------------')
      // this.getFormulaDic();

    })
  },



  getModeIdList: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/comm/beanCrud/sys.DictVO/find',
      method: 'POST',
      data: {
        groupCode: "a$gds$iot_dye_color$mode",
        pageSize: 100,
      },
      loading: false
    }, (res) => {
      var list = res.list
      if (this.data.id) {
        list.map((item, idx) => {
          if (item.id == this.data.cententDetail.modeId) {
            console.log(idx, '**************************')
            this.setData({
              modeIdIndex: idx
            })
          }
        })
      }


      this.setData({
        modeIdList: res.list
      })
      console.log(res, 'modelList')
    })
  }







})